<template>
  <div class="login-view">
    <div class="container">
      <div class="title">
        <h2>光谷智慧交通管理平台</h2>
        <img src="../../assets/image/login2.png" alt="" />
      </div>
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        status-icon
        :rules="rules"
        label-width="60px"
        class="demo-ruleForm"
        label-position="top"
      >
        <!-- <h2>光谷智慧交通系统</h2> -->
        <h3 class="title">用户登录</h3>
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="ruleForm.username"
            placeholder="请输入您的用户名"
          />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="ruleForm.password"
            type="password"
            autocomplete="off"
            placeholder="请输入您的用户密码"
            show-password
          />
        </el-form-item>

        <el-form-item label="角色" prop="type">
          <el-radio-group v-model="ruleForm.type">
            <el-radio label="common">普通用户</el-radio>
            <el-radio label="traffic">交通部门</el-radio>
            <el-radio label="admin" value="">管理员</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item>
          <el-button
            class="login-btn"
            type="primary"
            @click="submitForm(ruleFormRef)"
          >
            登录
          </el-button>
          <div class="btn2">
            <el-button
              key="primary"
              type="primary"
              text
              @click="jumpRegister()"
            >
              新用户注册
            </el-button>
            <el-button
              key="primary"
              type="primary"
              text
              @click="jumpResetPassword()"
              >修改密码</el-button
            >
          </div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ElMessage } from "element-plus";
import { UserHttp } from "../../request/UserHttp.js";
const $router = useRouter();
// 表单dom 表单数据
const ruleFormRef = ref();
const ruleForm = reactive({
  username: "",
  password: "",
  type: "common",
});

// 表单规则
const checkName = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("请输入用户名"));
  } else {
    callback();
  }
};
const validatePass = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("请输入密码"));
  } else {
    // 设置正则表达式 密码为6位数字
    let reg = /\d{6}/;
    let res = reg.test(value);
    if (!res) {
      callback(new Error("请输入6位数字"));
    } else {
      callback();
    }
  }
};
const rules = reactive({
  username: [{ validator: checkName, trigger: "blur" }],
  password: [{ validator: validatePass, trigger: "blur" }],
});

// 登录 提交/重置表单
const submitForm = (formEl) => {
  if (!formEl) return;
  formEl.validate(async (valid) => {
    // 表单验证 valid有效
    if (valid) {
      // api验证登录信息 后台是否有该用户信息
      let resLogin = await UserHttp.LoginUserHttp(ruleForm);
      console.log(resLogin, 777);
      // 验证成功(后台有该用户数据) 登录成功 返回首页
      if (resLogin.data.code === 200) {
        // 已自动修改用户登录状态isOnline
        // 保存用户数据到本地/状态管理，保存token  以便系统其他功能使用
        let userInfo = ruleForm;
        localStorage.setItem("userInfo", JSON.stringify(userInfo));
        let token = resLogin.data.token;
        localStorage.setItem("token", token);
        ElMessage({
          message: "登录成功",
          type: "success",
        });
        setTimeout(() => {
          $router.push("/home");
        }, 500);
      } else {
        // 验证失败(后台没有该用户数据)
        ElMessage.error("登录失败:用户名或密码错误");
      }
    } else {
      // 表单验证 valid无效
      return false;
    }
  });
};

// 注册
const jumpRegister = () => {
  $router.push("/register");
};

// 修改密码
const jumpResetPassword = () => {
  $router.push("/resetPassword");
};
</script>

<style lang="scss" scoped>
.login-view {
  position: relative;
  width: 100vw;
  height: 100vh;
  background-color: #def5fe;
  background-image: url("../../assets/image/login1.jfif");
  background-size: cover;
  .container {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    display: flex;
    gap: 150px;
    .title {
      text-align: center;
      h2 {
        padding-block: 20px;
      }
      img {
        width: 390px;
      }
    }
  }
  .el-form {
    width: 350px;
    padding: 20px 40px;
    background-color: #fff;
    border: 1px #eee solid;
    border-radius: 10px;
    .title {
      padding-bottom: 20px;
      text-align: center;
    }
    .el-radio-group {
      padding-block: 5px;
      .el-radio {
        :deep(.el-radio__label) {
          font-size: 12px;
        }
      }
    }
    .el-button.login-btn {
      width: 100%;
    }
    .btn2 {
      display: flex;
      width: 100%;
      margin-top: 10px;
      justify-content: space-between;
    }
  }
}
</style>
